<?php
namespace dwc_app;

function create_temp_dir()
{
    $filepath = DOC_ROOT ."/tmp/dir_". random_digits(5);
    // make sure the name is unique
    while(glob($filepath))
    {
        $filepath = DOC_ROOT ."/tmp/dir_". random_digits(5);
    }
    mkdir($filepath);
    return $filepath;
}

function random_digits($number, $start = 0)
{
    if(!$number) return null;
    // adding a 1 in front so we can get zeros in the first position
    $start = "1".$start.str_repeat(0, $number-1);
    $end = "1".str_repeat(9, $number);
    $random = rand($start, $end);
    // chop off the 1 to get the real random number
    return substr($random, 1);
}

function get_remote_file($remote_url, $download_wait_time = DOWNLOAD_WAIT_TIME, $timeout = DOWNLOAD_TIMEOUT_SECONDS)
{
    $context = stream_context_create(array('http' => array('timeout' => $timeout)));
    
    $file = @file_get_contents($remote_url, 0, $context);
    usleep($download_wait_time);
    
    $attempts = 1;
    while(!$file && $attempts < DOWNLOAD_ATTEMPTS)
    {
        $file = @file_get_contents($remote_url, 0, $context);
        usleep($download_wait_time);
        $attempts++;
    }
    unset($context);
    
    return $file;
}

function get_hashed_response($url, $download_wait_time = DOWNLOAD_WAIT_TIME)
{
    $response = get_remote_file($url, $download_wait_time);
    
    $hash = simplexml_load_string($response);
    
    return $hash;
}

function import_decode($string, $remove_shitespace = false, $decode = true)
{
    if($decode)
    {
        $string = str_replace('&nbsp;',  ' ', $string);
        $string = htmlspecialchars_decode(html_entity_decode($string));
    }
    
    $string = str_replace(" ", " ", $string);
    //utf-8 0x0A (nobreak space) does not get inserted into mysql properly, we change it back to &nbsp; 
    //$string = str_replace("\xA0", "&nbsp;", $string);
    $string = str_replace("\x0A", "&nbsp;", $string);
    
    if($remove_shitespace) $string = remove_whitespace($string);
    return trim($string);
}

function remove_whitespace($string)
{
    while(preg_match("/  /",$string)) $string = trim(str_replace("  "," ",$string));
    return trim($string);
}

function http_post($url, $parameters = array(), $headers = array())
{
    return http_request("post", $url, $parameters, $headers);
}

function http_get($url, $headers = array())
{
    return http_request("get", $url, array(), $headers);
}

function http_request($method, $url, $parameters = array(), $headers = array())
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    if($method == "post") curl_setopt($ch, CURLOPT_POST, true);
    if(isset($parameters) && $parameters) curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
    if($headers) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
    curl_setopt($ch, CURLOPT_TIMEOUT, 180);
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    if(isset($_SERVER['SERVER_NAME'])) curl_setopt($ch, CURLOPT_REFERER, $_SERVER['SERVER_NAME']);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1");
    
    $result = curl_exec($ch);
    
    // echo "url: $url<br>";
    // print_r($parameters);
    // print_r($headers);
    if(0 == curl_errno($ch))
    {
        curl_close($ch);
        return $result;
    }
    echo "url: $url<br>";
    echo "<br>$result<br>";
    print_r($parameters);
    print_r($headers);
    throw new \Exception("Curl exception: ". curl_error($ch));
}

?>